package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Banji;
import entity.Banji_PageBean;
import entity.Student;
import util.JDBCUtil;
import service.IBanjiService;
import service.IStudentService;
import service.impl.BanjiServiceImpl;
import service.impl.StudentServiceImpl;

@WebServlet("/banji")
public class BanjiServlet extends HttpServlet {
	private IBanjiService banjiService = new BanjiServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// /JavaWeb1907/banji?method=pageList
		// /JavaWeb1907/banji?method=delteById&id=12
		// /JavaWeb1907/banji?method=insert
		req.setCharacterEncoding("utf-8");
		String method = req.getParameter("method");
		if (null == method || "".equals(method)) {
			method = "pageList";
		}
		switch (method) {
		case "pageList":
			pageList(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "search":
			search(req, resp);
			break;
		case "toUpdate":
			toUpdate(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		default:
			System.out.println("default");
			break;
		}

	}

	private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String updateId =  req.getParameter("id");
		String updatename = req.getParameter("name");
		Banji banji = new Banji(Integer.parseInt(updateId),updatename);
		banjiService.update(banji);
		req.setAttribute("banji", banji);
		resp.sendRedirect(req.getContextPath() + "/banji?method=pageList");
	}

	private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String updateId =  req.getParameter("id");
		Banji banji = banjiService.selectById(Integer.parseInt(updateId));
		// 放到一块内存区域里面
		req.setAttribute("banji", banji);
		req.getRequestDispatcher("/banji_update.jsp").forward(req, resp);		

	}

	private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		String searchName = req.getParameter("searchName");
		List<Banji> list =banjiService.search(searchName);
		req.setAttribute("list", list);
		req.getRequestDispatcher("/banji_list.jsp").forward(req, resp);
	}

	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		System.out.println("BanjiServlet.insert()");
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		Banji banji = new Banji(Integer.parseInt(id),name);
		banjiService.insert(banji);
		resp.sendRedirect(req.getContextPath() + "/banji?method=pageList");
	}

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("BanjiServlet.delteById()");
		String id = req.getParameter("id");
		banjiService.deleteById(Integer.parseInt(id));
		// 删除完了之后重定向到列表界面再展示所有数据
		resp.sendRedirect(req.getContextPath() + "/banji?method=pageList");
	}

	private void pageList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String pageNo = req.getParameter("pageNo");
		String pageSize = req.getParameter("pageSize");
		if (null == pageNo || "".equals(pageNo)) {
			pageNo = "1";
		}
		if (null == pageSize || "".equals(pageSize)) {
			pageSize = "10";
		}

		Banji_PageBean pageBean = banjiService.pageList(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
		
		req.setAttribute("pageBean", pageBean);
		req.getRequestDispatcher("banji_page_list.jsp").forward(req, resp);
	}

}
